package com.android.jianboshi.ui.activity;

import android.os.Build;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.tool.Log;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.jianboshi.R;
import com.android.jianboshi.adapter.LogAdapter;
import com.android.jianboshi.bean.LogInfo;
import com.android.jianboshi.constants.Constants;
import com.android.jianboshi.event.Event_Notification;
import com.blankj.utilcode.util.ConvertUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class TestActivity1 extends BaseActivity {

    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.tv_cishu)
    TextView tv_cishu;

    List<LogInfo> logInfoList = new ArrayList<>();
    LogAdapter adapter;
    boolean isStop = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_test1;
    }

    @Override
    protected void initView() {
        comm_title.setTitle("测试2");

        comm_title.setRightText("暂停", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isStop = !isStop;
                if (isStop) {
                    comm_title.setRightText("继续");
                } else {
                    comm_title.setRightText("暂停");
                }
            }
        });

        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new LogAdapter(mContext, null);
        recyclerview.setAdapter(adapter);
        adapter.setData(logInfoList);

    }

    @Override
    protected void initData() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event_Notification event) {
        if (!isStop) {
            parseScanResult(event.data);

            Log.i("onScanResult3");
        }
    }


    @Override
    protected boolean isRegistEventbus() {
        return true;
    }

    /**
     * 解析扫描结果
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void parseScanResult(byte[] data) {
        byte byte2 = data[10+ Constants.length];  //序号
        byte byte3 = data[14+ Constants.length];  //气压高字节
        byte byte4 = data[15+ Constants.length];  //气压低字节
        byte byte5 = data[16+ Constants.length];  //打开采样次数
        //气压值
        int byte1_int = ConvertUtils.hexString2Int(ConvertUtils.bytes2HexString(new byte[]{byte3, byte4}));
//        int byte2_int = ConvertUtils.hexString2Int(ConvertUtils.bytes2HexString(new byte[]{byte4}));
        int samplingTimes = ConvertUtils.hexString2Int(ConvertUtils.bytes2HexString(new byte[]{byte5}));

        double airPressure = byte1_int / 100d;


        LogInfo logInfo = new LogInfo();
        logInfo.time = System.currentTimeMillis();
        logInfo.airPressure = airPressure;
        logInfo.hex = ConvertUtils.bytes2HexString(data);
        logInfo.samplingTimes = samplingTimes;
        logInfo.num = byte2;

        logInfoList.add(logInfo);

        adapter.notifyDataSetChanged();
        recyclerview.scrollToPosition(logInfoList.size() - 1);

        tv_cishu.setText("收到数据：" + logInfoList.size() + "条");

    }

}
